package ink.yql.service.Impl;

import ink.yql.dao.ProjectDao;
import ink.yql.dao.ProjectUpdateDao;
import ink.yql.model.Project;
import ink.yql.model.ProjectUpdate;
import ink.yql.service.ProjectService;
import ink.yql.util.RestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yql.myutil.MyDate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectDao projectDao;
    @Autowired
    private ProjectUpdateDao projectUpdateDao;
    @Autowired
    private HttpServletRequest req;

    @Override
    public RestObject getByPid(Integer pid) {
        if (pid == null) {
            return RestObject.builder().message("请传输正确的数据").code("400").build();
        }
        Project byPid = projectDao.getByPid(pid);
        if (byPid == null) {
            return RestObject.builder().message("该项目不存在").code("404").build();
        }
        return RestObject.builder().code("200").message("查询成功").data(byPid).build();
    }

    @Override
    public RestObject getByStatus(String lable, String pstatus, String orderBy) {
        if (pstatus == null) {
            return RestObject.builder().code("400").message("请传输一个正确的数据").build();
        }
        List<Project> list = projectDao.getByStatus(lable, Integer.parseInt(pstatus), orderBy);
        if (orderBy != null && !orderBy.equals("1") && !orderBy.equals("2")) {
            //TODO 评论的多少来排序
        }
        return RestObject.builder().code("200").message("查询成功").data(list).build();
    }

    @Override
    public RestObject addOne(Project project) {
        if (project == null){
            return RestObject.builder().code("400").message("请传输正确的数据").build();
        }
        Integer pstatus = project.getPstatus();
        if (pstatus != 1 && pstatus != 0){
            return RestObject.builder().code("400").message("请传输正确的数据").build();
        }
        projectDao.addOne(project);

        return RestObject.builder().code("200").message("添加项目成功").build();
    }

    @Override
    public RestObject update(Project project) {
        if (project == null){
            return RestObject.builder().code("400").message("请传输正确的数据").build();
        }
        Project byPid = projectDao.getByPid(project.getPid());
        if (byPid == null){
            return RestObject.builder().code("404").message("请传输正确的数据,该项目不存在").build();
        }
        if (byPid.getPstatus() != 0){
            return RestObject.builder().code("400").message("项目已发布不可修改").build();
        }
        projectDao.update(project);
        return RestObject.builder().code("200").message("修改成功").build();
    }

    @Override
    public RestObject addUpdate(ProjectUpdate projectUpdate) {
        try{
            Integer max = projectUpdateDao.getMaxPufrequencyByPid(projectUpdate.getPid());
            projectUpdate.setPufrequency(max+1);
            projectUpdate.setPupdateTime(new MyDate().toString());
            projectUpdateDao.addOne(projectUpdate);
            return RestObject.builder().code("200").message("添加项目更新成功").build();
        }catch (Exception e){
            return RestObject.builder().code("404").message("添加项目更新失败").build();
        }

    }

    @Override
    public RestObject getUpdateByPid(Integer pid) {
        List<ProjectUpdate> byPid = projectUpdateDao.getByPid(pid);
        return RestObject.builder().code("200").message("查询项目更新成功").data(byPid).build();
    }
}
